﻿using UnityEngine;

namespace GCFramework.Singleton
{
    public class SingletonMono<T> : MonoBehaviour where T : SingletonMono<T>
    {
        private static readonly object LockObject = typeof(T);
        private static volatile T _instance;

        public static T Ins
        {
            get
            {
                if (_instance == null)
                {
                    // 进入锁定
                    lock (LockObject)
                    {
                        _instance = FindObjectOfType<T>();
                        if (_instance == null)
                        {
                            // 第二次检查，防止在获取锁之前已经有其他线程创建了实例
                            _instance = new GameObject("Singleton Of " + typeof(T))
                                .AddComponent<T>();
                        }
                    }
                }

                return _instance;
            }
        }
        
        [Header("是否作为全局单例")]
        public bool isGlobal = true;

        private void Register()
        {
            if (_instance == null)
            {
                _instance = (T)this;
            }
            else if(_instance != this)
            {
#if UNITY_EDITOR
                Debug.Log($"{typeof(T)} 单例重复");
#endif
                Destroy(gameObject);
                return;
            }
            
            if(isGlobal)
                DontDestroyOnLoad(gameObject);
        }

        protected virtual void Awake() => Register();
        
        public static bool IsInitialized => _instance != null;

        protected virtual void OnDestroy()
        {
            if (_instance == this)
                _instance = null;
        }
    }
}